package cn.com.huzo.webapp.controller.financial;

import cn.com.huzo.model.entities.financial.AccoutingPeriodEntity;
import cn.com.huzo.service.financial.AccoutingPeriodManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 会计期画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/financial/accouting_period")
public class AccoutingPeriodController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "AccoutingPeriodManager")
    private AccoutingPeriodManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到会计期一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("financial.accouting_period.list.def");
        return view;
    }

    /**
     * 跳转到会计期参数设置画面
     *
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/options.html")
    public ModelAndView redirectOptionsPopupWnd() throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.setViewName("popup.financial.accouting_period.options.def");
        return view;
    }

    /**
     * 设置会计期生成的条件画面
     *
     * @return
     */
    @RequestMapping("/criteria/edit.html")
    public ModelAndView redirectNewLinesWizardWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.financial.accouting_period_edit.wizard.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得最后设置的会计年度。
     *
     * @return 期间数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchLastApYear.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchLastApYear() throws Exception {
        return _manager.fetchLastApYear();
    }

    /**
     * 取得该会计年度的期间数据。
     *
     * @param criterion 会计年度
     * @return 期间数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByApYear.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<AccoutingPeriodEntity> fetchByApYear(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByApYear(MapUtils.getString(criterion, "ApYear"));
    }

    /**
     * 增加会计期数据。
     *
     * @param itemList 要处理的数据列表
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer bulkInsert(@RequestBody final List<Map<String, Object>> itemList) throws Exception {
        return _manager.bulk_insert(itemList);
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }
    // </editor-fold>
}
